<?php
/**
 * 进云快速开发框架
 * @apple
 */
define('NOSESSION',true);
require '../../core/core.php';
set_exi(function($message='OK',$type='SUCCESS'){
	//如果存在错误，此处整理错误日志，后期完善
	if($type=='error'){
		$log=date('Y/m/d H:i:s');
		$log.="\t{$_SESSION['uniacid']}\t{$_SESSION['uid']}\t{$message}\n";
		file_put_contents('kspay_error_log.txt',$log,FILE_APPEND);
	}
	echo 'success';
	exit;
});
$input = file_get_contents('php://input');
$get=json_decode($input,true);
$data=json_decode($get['data'],true);
$pay_order=pdo_get('core_paylog',array('tid'=>trim($data['out_order_no'])));
if(!$pay_order){
	exi('指定订单不存在！','error');
}
if($pay_order['status']!=0){
	exi('订单已支付！','error');
}
//验证签名
$_SESSION['uniacid']=$pay_order['uniacid'];
$_SESSION['uid']=$pay_order['mid'];
if($pay_order['merchid']){
	$_SESSION['onemerchid']=$pay_order['merchid'];
}
$_SESSION['this']='custom';
$check=cfc('ks_pay')->check_sign($get);
if($check){
	if($data['trade_no']){
		//支付成功
		if($pay_order['di_credit'] && $pay_order['di_fee']>0){
			cfc('pay')->up_dipay($pay_order);
		}
		pdo_update('core_paylog',array('transaction_id'=>$data['trade_no'],'status'=>1,'pay_type'=>'kspay','paytime'=>time()),array('id'=>$pay_order['id']));
		$_SESSION['pay_result']=$_RQ['pay_result']=array('id'=>$pay_order['oid'],'transaction_id'=>$data['trade_no'],'status'=>1,'pay_type'=>'kspay','fee'=>$pay_order['fee'],'pay_id'=>$pay_order['id'],'pay_tid'=>$pay_order['tid'],'title'=>$pay_order['title']);
		access($pay_order['back_url']);
		exi('success','success');
	}
}else{
	exi('签名校验失败！','error');
}
?>